<template>
	<view>
		<uni-popup @maskClick="cose" ref="popup" background-color="#fff"
			style="border-radius: 10px;height:100vh;background-color: #e8ebb9;">


			<view class="popup-content" style="margin: 20px;border-radius: 8px;">
				<uni-title align="center" type="h2"
					:title="sysScheduleTimetable_dangqian.customerModel.student.name+'的'+zhunbeibianjideshuju.title"
					color="#5b5c5f">
				</uni-title>



				<scroll-view scroll-y style="height: 500px;margin-bottom: 40rpx;">
					<uni-forms labelWidth="300" style="background-color: #fff;">

						<view v-for="(item1, index) in drawerFindData">

							<uni-forms-item :label="item1.label" prop="">

								<view v-if="item1.children.length==1">
									<p v-show="false">
										{{ findList[index].find3 = item1.children[0] }}
									</p>
								</view>

								<view v-if="item1.children.length!=1">
									<uni-data-checkbox @change="asdfasd" :index2="index" :mapValue="true" mode="button"
										:map="{text:'label',value:'value'}" v-model="findList[index].find3" :key2="true"
										:localdata="item1.children"></uni-data-checkbox>
								</view>

								<view v-if="findList[index].find3!=null">
									<view v-if="findList[index].find3!==''">
										<view v-if="findList[index].find3.length!=19">
											<view v-if="findList[index].find3.children!=null">
												<view v-if="findList[index].find3.children[0].label.search('备注')!==-1">
													<uni-forms-item label="备注:" prop="">
														<uni-easyinput v-model="findList[index].findRemarks3"
															style="width: 100%">
														</uni-easyinput>
													</uni-forms-item>
												</view>
											</view>
										</view>
									</view>
								</view>


								<view
									v-if=" findList[index].find3!==''&&findList[index].find3!=null&&findList[index].find3.label!=null&&findList[index].find3.label.length!=19 ">
									<uni-forms-item labelWidth="300" v-if="findList[index].find3.label.search('备注')!=-1"
										label="备注:" prop="">
										<uni-easyinput v-model="findList[index].findRemarks3" style="width: 100%">
										</uni-easyinput>
									</uni-forms-item>
								</view>

								<view
									v-if=" findList[index].find3!==''&&findList[index].find3!=null&&findList[index].find3.label!=null&&findList[index].find3.label.length!=19&&findList[index].find3.label.search('自己填写')!==-1">
									<!--                  个-->
									<uni-forms-item labelWidth="300" v-if="findList[index].find3.label.search('个')!=-1"
										label="填写(个):" prop="">
										<uni-number-box v-model="findList[index].findRemarks3" :min="0"
											:max="Number(findList[index].find3.label.match(/\d+/g)[1])"
											style="width: 100%">
										</uni-number-box>
									</uni-forms-item>


									<!--                  自己备注-->
									<uni-forms-item labelWidth="300"
										v-else-if="findList[index].find3.label.search('自己备注')!=-1" label="备注:" prop="">
										<uni-easyinput v-model="findList[index].findRemarks3" style="width: 100%">
										</uni-easyinput>
									</uni-forms-item>
									<!--                  组-->
									<uni-forms-item labelWidth="300"
										v-else-if="findList[index].find3.label.search('组')!=-1 " label="填写(组):" prop="">
										<uni-number-box v-model="findList[index].findRemarks3" :min="0"
											:max="Number(findList[index].find3.label.match(/\d+/g)[1])"
											style="width: 100%">
										</uni-number-box>
									</uni-forms-item>
									<!--                  cm-->
									<uni-forms-item labelWidth="300"
										v-else-if="findList[index].find3.label.search('cm')!=-1 " label="填写(cm):"
										prop="">
										<uni-number-box v-model="findList[index].findRemarks3" :min="0"
											:max="Number(findList[index].find3.label.match(/\d+/g)[1])"
											style="width: 100%">
										</uni-number-box>
									</uni-forms-item>

									<!--                  圈-->
									<uni-forms-item labelWidth="300"
										v-else-if="findList[index].find3.label.search('圈')!=-1 " label="填写(圈):" prop="">
										<uni-number-box v-model="findList[index].findRemarks3" :min="0"
											:max="Number(findList[index].find3.label.match(/\d+/g)[1])"
											style="width: 100%">
										</uni-number-box>
									</uni-forms-item>
									<!--                  分钟-->
									<uni-forms-item labelWidth="300"
										v-else-if="findList[index].find3.label.search('分钟')!=-1 " label="填写(分钟):"
										prop="">
										<uni-number-box v-model="findList[index].findRemarks3" :min="0"
											:max="Number(findList[index].find3.label.match(/\d+/g)[1])"
											style="width: 100%">
										</uni-number-box>
									</uni-forms-item>

									<!--                  次-->
									<uni-forms-item labelWidth="300"
										v-else-if="findList[index].find3.label.search('次')!=-1 " label="填写(次):" prop="">
										<uni-number-box v-model="findList[index].findRemarks3" :min="0"
											:max="Number(findList[index].find3.label.match(/\d+/g)[1])"
											style="width: 100%">
										</uni-number-box>
									</uni-forms-item>

									<!--                  张-->
									<uni-forms-item labelWidth="300"
										v-else-if="findList[index].find3.label.search('张')!=-1 " label="填写(张):" prop="">
										<uni-number-box v-model="findList[index].findRemarks3" :min="0"
											:max="Number(findList[index].find3.label.match(/\d+/g)[1])"
											style="width: 100%">
										</uni-number-box>
									</uni-forms-item>


									<uni-forms-item v-else-if="findList[index].find3.label.search('秒')!=-1 "
										label="(xx分xx秒):" prop="">
										<p v-show="false">
											{{ findList[index].label = "时间" }}
										</p>
										<uni-easyinput v-model="findList[index].findRemarks3" style="width: 100%">
										</uni-easyinput>
										<span v-if="!validateTimeFormat(findList[index].findRemarks3)"
											style="color: red">
											时间只能是xx分xx秒的格式(例如 3分11秒)
										</span>

									</uni-forms-item>



									<!--               // :min="Number(findList[index].find3.label.match(/\d+/g)[0]) 获取最小次数-->


									<uni-forms-item v-else label="填写" prop="">
										<uni-number-box v-model="findList[index].findRemarks3" :min="0"
											:max="Number(findList[index].find3.label.match(/\d+/g)[1])"
											style="width: 100%">
										</uni-number-box>
									</uni-forms-item>

								</view>



							</uni-forms-item>

						</view>



						<uni-forms-item label="项目备注" prop="">
							<uni-easyinput autoHeight type="textarea" :min="2" v-model="findList[9].remarks"
								style="width: 100%">
							</uni-easyinput>
						</uni-forms-item>

						<view class="example-body" style="margin-bottom: 15px;">
							<uni-file-picker :sourceType="['camera']" limit="9" title="课堂记录照片" :del-icon="false"
								disable-preview :imageStyles="{
					width: 80,
					height: 80,
					border: {
						radius: '8px'
					},

				}" @select="select" @progress="progress" @success="success" @fail="fail" file-mediatype="image"
								v-model="fileLists">
								拍照</uni-file-picker>
						</view>


					</uni-forms>
				</scroll-view>

				<view class="uni-flex uni-row" style="display: flex;">
					<view class="text" style="width: 200rpx;">

						<button @click="cose">
							关闭
						</button>
					</view>
					<view class="text" style="-webkit-flex: 1;flex: 1;margin-left: 10px;">
						<button class="button" style="background-color: #F0C10C;color: #fff;" size="20"
							@click="doInsertDoInsert">保存</button>
					</view>
				</view>


			</view>


		</uni-popup>

		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
		</uni-popup>

	</view>
</template>

<script>
	import {
		doInsertDoInsert,
		getJsonfile,
		getListByfind1AdnSysScheduleTimetable
	} from "@/api/lpc/courserecord";
	import {
		getkcjldx
	} from "@/api/coreLessonPreparation/source";

	export default {
		data() {
			return {
				fileLists: [],
				formData: {},
				msgType: "",
				messageText: "",
				drawerData: {
					find1: null,
					id: null,
					customerId: null,
					customerModel: {
						student: {
							name: ''
						}
					}

				},
				findList2: [{
						sort: 1,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []
					},
					{
						sort: 2,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []
					},
					{
						sort: 3,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []

					}, {
						sort: 4,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []
					},
					{
						sort: 5,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []
					},
					{
						sort: 6,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []
					},
					{
						sort: 7,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []
					},
					{
						sort: 8,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []
					},
					{
						sort: 9,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []
					}, {
						sort: 10,
						find1: null,
						find2: '22222',
						find3: {
							value: '22222'
						},
						customerId: null,
						sysScheduleTimetable: null,
						remarks: '',
						img: [],
					}
				],

				findList: [{
						sort: 1,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []
					},
					{
						sort: 2,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []

					},
					{
						sort: 3,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []

					},
					{
						sort: 4,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []
					},
					{
						sort: 5,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []
					},
					{
						sort: 6,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []
					},
					{
						sort: 7,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []
					},
					{
						sort: 8,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []
					},
					{
						sort: 9,
						customerId: null,
						sysScheduleTimetable: null,
						find1: null,
						findRemarks1: null,
						find2: null,
						findRemarks2: null,
						find3: null,
						findRemarks3: null,
						find4: null,
						findRemarks4: null,
						ids: []
					},
					{
						sort: 10,
						find1: null,
						find2: '22222',
						find3: {
							value: '22222'
						},
						customerId: null,
						sysScheduleTimetable: null,
						remarks: null,
						img: [],
					}
				],
				drawerFindData: [],
				formData: {}, //要提交的数据
				radio2: 0,
				radio3: 0,
				t: "1",
				zhunbeibianjideshuju: {
					xiangmu_data: {
						name: "",
						code: ""
					}
				},
				sysScheduleTimetable_dangqian: {
					customerModel: {
						student: {
							name: ""
						}
					}
				},
				sex: [{
					text: '正常',
					value: 0
				}, {
					text: '否定',
					value: 1
				}, {
					text: '失败',
					value: 2
				}],
			}
		},
		methods: {
			iconClick(e) {
				console.log(e);
			},
			change(e) {
				console.log(e);
			},
			/**
			 * 验证时间格式
			 * @param input
			 * @returns {boolean}
			 */
			validateTimeFormat(input) {
				const regex = /^\d{1,2}分\d{1,2}秒$/; // 匹配 xx分xx秒 格式的正则表达式
				const regex2 = /^\d{1,2}秒$/;
				return regex.test(input) || regex2.test(input);
			},
			// 上传函数
			async uploadFiles(tempFilePaths, i) {
				let that = this

				await uni.uploadFile({
					url: "https://api3.m.grasswave.cn/opsli-boot/api/v1/tools/oss/upload", //后端用于处理图片并返回图片地址的接口    
					filePath: tempFilePaths[i],
					name: 'file',
					header: {
						"x-token": uni.getStorageSync("x-token"),
					},
					success: res => {
						let data = JSON.parse(res.data) //返回的是字符串，需要转成对象格式   
						if (data.code == 200) {
							uni.showToast({
								title: "上传成功",
								icon: "none",
								duration: 500
							})
							this.fileLists.push({
								name: data.data.fileStoragePath,
								url: data.data.fileStoragePath,
								extname: "jpg"
							})
							console.log(this.fileLists)
						}
					},
					fail: () => {}
				})

			},
			// 获取上传状态
			select(e) {
				let promises = []
				for (let i = 0; i < e.tempFilePaths.length; i++) {
					const promise = this.uploadFiles(e.tempFilePaths, i)
					promises.push(promise)
				}
				Promise.all(promises).then(() => {

				})
			},
			// 获取上传进度
			progress(e) {},

			// 上传成功
			success(e) {
				console.log(e);
			},

			// 上传失败
			fail(e) {
				console.log('上传失败：', e)
			},
			// 保存

			asdfasd(e) {

				this.findList[e.index2].find3 = e.detail.data

				this.$forceUpdate() //这不会更新您拥有的任何计算属性,调用forceUpdate只会强制重新渲染视图
				// console.log(this.findList[e.index2].find3);
			},
			cose() {
				Object.assign(this.$data, this.$options.data())
				this.$refs.popup.close()

				this.$emit("fetchData");
			},

			// 开始
			async open(sysScheduleTimetable_dangqian, zhunbeibianjideshuju) {
				this.$refs.popup.open("bottom")
				Object.assign(this.$data, this.$options.data())

				this.sysScheduleTimetable_dangqian = sysScheduleTimetable_dangqian
				this.zhunbeibianjideshuju = zhunbeibianjideshuju

				this.drawerData = this.sysScheduleTimetable_dangqian
				let ids

				// this.findList.lastId = null
				// this.findList.lastId = this.drawerData[zhunbeibianjideshuju.xiangmu_data.code].split(",")[1]
				if (sysScheduleTimetable_dangqian.lessonPreparationRecordsModelList !== null &&
					sysScheduleTimetable_dangqian.lessonPreparationRecordsModelList.length != 0) {
					//从row.lessonPreparationRecordsModelList里找到元素type是lable的 元素的ids
					for (const datum of sysScheduleTimetable_dangqian.lessonPreparationRecordsModelList) {
						if (datum.type == zhunbeibianjideshuju.type) {
							this.beikeId = datum.id
							console.log(JSON.parse(datum.beikeId));

							ids = JSON.parse(datum.beikeId).toString()
							console.log(ids.split(",")[0]);
						}
					}
					if (ids == null) {
						this.msgType = "warn"
						this.messageText = "数据异常，请联系技术"
						this.$refs.message.open()
						return
					}

					// console.log(ids);
				} else {
					//老版在这儿获取ids
				}

				this.drawerData.find1 = ids.split(",")[0]
				// 1580131532259991553,1557005191901274113,1557005069326934017,1557004896693575681

				//获取历史记录
				await this.getkechengjilu(ids.split(",")[0], this.drawerData.id)

				const {
					data,
					msg,
					success
				} = await getkcjldx({
					ids: ids,
					offlineClassName: this.drawerData.offlineClassName
				})

				if (success) {

					this.drawer = true


					uni.showToast({
						title: msg,
						icon: "none",
						duration: 500
					})

					this.drawerFindData = data

					for (const datum of data) {

						datum.find2 = datum.value
					}





					for (let i = 0; i < this.drawerFindData.length; i++) {
						if (this.drawerFindData[i].children.length != 1) {
							for (const datum of this.drawerFindData[i].children) {
								if (this.findList[i].find3 === datum.value) {
									this.findList[i].find3 = datum
								}
							}
						}

					}


					this.fileLists = []

					if (this.findList[9].img != null && this.findList[9].img.length != 0) {
						// console.log(this.findList[9].img);
						for (const datum of this.findList[9].img.split(",")) {
							if (datum != "") {
								this.fileLists.push({
									url: datum,
									extname: 'jpg',
									name: datum
								})
							}
						}
					}

					// console.log(this.fileLists);

				}



				this.t = "2"
			},
			adfadf(index, arr) {
				for (const arrElement of arr) {
					if (arrElement.value === index) {

						return

					}
				}
			},

			gaibian(a, b) {
				this.findList[a].find3 = b
			},
			async doInsertDoInsert() {
				this.drawer = false
				// this.findList.LastId =
				const a = JSON.parse(JSON.stringify(this.findList));

				for (const aElement of a) {
					if (aElement.label === '时间' && !this.validateTimeFormat(aElement.findRemarks3)) {
						this.msgType = "warn"
						this.messageText = aElement.findRemarks3 + "不是要求的时间格式,请检查"
						this.$refs.message.open()
						// this.$baseMessage(aElement.findRemarks3 + "不是要求的时间格式,请检查", "error");
						return
					}
				}
				let fasdfasdf = []

				for (const datum of this.fileLists) {
					fasdfasdf.push(datum.url)
				}
				a[9].img = fasdfasdf.toString()

				for (const datum of a) {
					datum.customerId = this.drawerData.customerId
					datum.sysScheduleTimetable = this.drawerData.id
					datum.find1 = this.drawerData.find1
					if (datum.find3 != null && datum.find3.length != 19) {
						datum.find3 = datum.find3.value
					}
					datum.ids = this.ids
					datum.beikeId = this.beikeId

				}
				const {
					data,
					msg
				} = await doInsertDoInsert(a)
				this.drawerFindData = null
				this.$refs.popup.close()
				this.$emit("refreshData", this.drawerData.id, this.zhunbeibianjideshuju.id);
			},

			handleRemove(file, fileList) {},
			handlePictureCardPreview(file) {
				this.findList[9].img = file.url;
				this.dialogVisible = true;
			},

			error(esponse) {
				this.$baseMessage("文件上传失败", "error");
			},


			//方法
			async getkechengjilu(find1, sysScheduleTimetable) {
				this.findList = JSON.parse(JSON.stringify(this.findList2));
				const {
					data,
					msg
				} = await getListByfind1AdnSysScheduleTimetable({
					find1: find1,
					sysScheduleTimetable: sysScheduleTimetable
				})


				if (data.length > 0) {

					for (let i = 0; i < data.length; i++) {

						if (data[i].find2 == "22222") {


							this.findList[9].remarks = data[i].remarks
							this.findList[9].img = data[i].img
							this.findList[9].id = data[i].id
						} else {


							// if (this.findList[i]) {

							this.findList[i].id = data[i].id
							this.findList[i].find1 = data[i].find1
							this.findList[i].find2 = data[i].find2
							this.findList[i].find3 = data[i].find3
							this.findList[i].find4 = data[i].find4

							this.findList[i].findRemarks1 = data[i].findRemarks1
							this.findList[i].findRemarks2 = data[i].findRemarks2
							this.findList[i].findRemarks3 = data[i].findRemarks3
							this.findList[i].findRemarks4 = data[i].findRemarks4
							// }

						}
					}


				} else {
					this.msgType = "warn"
					this.messageText = "没有历史数据"
					this.$refs.message.open()
				}


			},
		}
	}
</script>

<style>
	/deep/.uni-forms-item {
		margin-bottom: 1px;
	}
</style>